﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Resources;
using System.Reflection;
using System.Globalization;
using System.Threading;

namespace Shsc.Net.Common.Resource
{
    public class MessageUtil
    {
        private static ResourceManager GetCurrentCulture(string resource)
        {
            ResourceManager rm =
                new ResourceManager(resource, Assembly.GetExecutingAssembly());
            return rm;
        }

        public static string getLabel(string labelId, string language, string resource)
        {
            string message = "";
            try
            {
                CultureInfo ci = null;
                ResourceManager rm = GetCurrentCulture(resource);
                if (language == "")
                {
                    ci = Thread.CurrentThread.CurrentCulture;
                }
                else
                {
                    ci = new CultureInfo(language);
                }
                if (language == "")
                {
                    message = rm.GetString(labelId);
                }
                else
                {
                    message = rm.GetString(labelId, ci);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return message;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="msgId"></param>
        /// <param name="language"></param>
        /// <param name="resource"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string getMessage(string msgId, string language, string resource,
            params string[] param)
        {
            string message = getLabel(msgId, language, resource);
            if (param != null && param.Length > 0)
            {   
                return string.Format(message, param);
            }
            else
            {
                return message;
            }
        }
    }
}
